package hlhj.fhp.checkpoint.http

import android.content.Context
import android.content.Intent
import android.util.Log
import com.example.mymvp.log
import com.example.mymvp.mvp.BaseFgm

import com.example.mymvp.okrx.BaseAciton
import com.example.mymvp.okrx.MyException
import com.lzy.okgo.model.Response

import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.atys.LoginAty
import hlhj.fhp.checkpoint.utils.LoadingUtls
import hlhj.fhp.checkpoint.utils.SPHelper
import hlhj.fhp.checkpoint.utils.ToastUtils
import io.reactivex.disposables.Disposable

/**
 * Created by Never Fear   on 2018\11\6 0006.
 * Never More....
 */

abstract class  CustomAction<T>(private val context: Context) : BaseAciton<T> {
    override fun httpFailed(e: Throwable?) {
        log("okgo","到错误了？")
        if (e is MyException) {
            ToastUtils.toast(e.message!!,context)
            if (e.code==0){
                if (e.message?.contains("已过期")!!){
                    SPHelper.saveToken(context,"")//清空token
                    if (context is BaseAty<*,*>){
                            context?.startActivity(Intent(context,LoginAty::class.java))
                            context?.finish()
                    }
                }
            }
            // code错误
        } else {
            ToastUtils.toast(e!!.message!!,context)
            // 请求报错 x
        }
    }

    override fun acheSuccess(t: T) {


    }

    override fun onComplete() {
        LoadingUtls.diss()

    }
    override fun onSubscribe(d: Disposable) {
        if (context is BaseAty<*,*>){
            context.addDisable(d)
        }else if (context is BaseFgm<*,*>){
            context.addDisable(d)
        }
    }
}
